﻿<?php

   header("Content_Type: text/comma-separated-values");
	//Nom du fichier en sortie
   $fileName = 'export_structures_'.date("m.d.y_G\hi").'.csv';
	header('Content-disposition: attachement; filename="'.$fileName.'"; charset=UTF-8');
	header("Content-Type: application/csv-tab-delimited-table");

   // la localisation est redéfinie au cas où la configuration du serveur ne serait pas bonne
	setlocale(LC_ALL, "fr_FR");
	
	// les variables php sont encodées en UTF-8
	mb_internal_encoding("UTF-8");
	
	// lors de l'utilisation d'expression régulières, celles-ci seront encodées en UTF-8
	mb_regex_encoding("UTF-8");
   
   // Inclusion fichier de configuration
	require_once("../../config.php");
	
	// Inclusion les fonctions php externes
	require_once("../../librairies/fonctions.php");

   $connexion = connexionBDD($bdd_hostname, $bdd_login, $bdd_password, $bdd_database);
	
	if($connexion === false){
		die("ERREUR: connexion impossible à la base de données");
	}
   
   
/**Construction de la requete multicritères**/

   $entete = array();
   $cptEntete = 0;
   
   
   $query_select = "SELECT DISTINCT st.CODEJB AS id, st.NOMSTRUCTURE as nom ";
   
	$query_jointures = "FROM structure st ";
   
     
   $entete[0] = "Numcor";
   $entete[1] = "Nom";
   $cptEntete=2;
   
   $flag = false;
  
   $query_where = "WHERE ";
   
   if($_POST['withVille'] != ""){
      $query_jointures .= "INNER JOIN commune c ON c.CODECOMMUNE = st.CODECOMMUNE ";
      $query_where .= "c.NOMCOMMUNE = \"".$_POST['withVille']."\" ";
      $flag = true;
   }
   
   if($_POST['withPays'] != ""){
      if ($flag){
         $query_where .= "AND ";
      }
      $query_jointures .= "INNER JOIN pays p ON p.CODEPAYS = st.CODEPAYS ";
      $query_where .= "p.NOMPAYS = \"".$_POST['withPays']."\" ";
      $flag = true;
   }
   
   if($_POST['lastCsup'] != ""){
      if ($flag){
         $query_where .= "AND ";
      }
      $query_where .= "st.DATEDERNIERCONTACT >= \"".$_POST['LastCsup']."\" ";
   }
   
   $flag=false;
   
   if(isset($_POST['Adresse'])) {
      $entete[$cptEntete] = "Adresse";
      $cptEntete++;
      $query_select .= ", ADRESSESTRUCTURE as adresse ";
   }
   
   if(isset($_POST['CP'])) {
      $entete[$cptEntete] = "Code Postal";
      $cptEntete++;
      $query_select .= ", CODECOMMUNE as idVille ";
      $flag=true;
   }
   
   if(isset($_POST['Ville'])) {
      $entete[$cptEntete] = "Ville";
      $cptEntete++;
      if(!$flag){
         $query_select .= ", CODECOMMUNE as idVille ";
      }
   }
   
   if(isset($_POST['Pays'])) {
      $entete[$cptEntete] = "Pays";
      $cptEntete++;
      if(!$flag){
         $query_select .= ", CODECOMMUNE as idVille ";
      }
   }
   
   if(isset($_POST['LastC'])){
      $entete[$cptEntete] = "Date de Dernier Contact";
      $cptEntete++;
      $query_select .= ", DATEDERNIERCONTACT as lastC ";
   }
   
   if(isset($_POST['Tel'])){
      $entete[$cptEntete] = "N° de Téléphone";
      $cptEntete++;
      $query_select .= ", TELEPHONESTRUCTURE as tel ";
   }
   
   if(isset($_POST['Fax'])){
      $entete[$cptEntete] = "N° de Fax";
      $cptEntete++;
      $query_select .= ", FAXSTRUCTURE as fax ";
   }
   
   if(isset($_POST['Email'])){
      $entete[$cptEntete] = "Email";
      $cptEntete++;
      $query_select .= ", EMAILSTRUCTURE as email ";
   }
   
   if(isset($_POST['Web'])){
      $entete[$cptEntete] = "Site Internet";
      $cptEntete++;
      $query_select .= ", SITEINTERNET as web ";
   }

   
   $query_get_structures = $query_select.$query_jointures.($query_where!="WHERE "?$query_where:"")."ORDER BY id ";
   $time_start = microtime(true);
      
   $result_get_structures = mysql_query($query_get_structures, $connexion) or logError("RECUPERATION  STRUCTURES-".$query_get_structures."-".mysql_error(),"../..");
   
   $compte = 0;
   
   if ($result_get_structures) {
   
	   while($tab = mysql_fetch_assoc($result_get_structures)){
	      
	      $tab_get_exp[$compte]['id'] = $tab['id'];
	      $tab_get_exp[$compte]['nom'] = $tab['nom'];
	      if(isset($_POST['Adresse'])){
	         $tab_get_exp[$compte]['adresse'] = $tab['adresse'];
	      }
	      
	      if(isset($_POST['Ville']) || isset($_POST['CP'])){
	      
	         $query_ville = "SELECT ".(isset($_POST['CP'])?"CODEPOSTAL as cp, ":"").(isset($_POST['Ville'])?"NOMCOMMUNE as nom ":"")."FROM commune c ";
	         $query_ville .= "WHERE CODECOMMUNE = ".$tab['idVille']." ";
	      
	         $result_ville = mysql_query($query_ville, $connexion) or logError("RECUPERATION  VILLE-".$query_ville."-".mysql_error());
	         
	         $tab_ville = false;
	         
	         if(isset($_POST['Ville'])){
	            while($val = mysql_fetch_assoc($result_ville)){
	               $tab_ville = $val['nom'];
	            }
	         
	            $tab_get_exp[$compte]['nomCommune'] = $tab_ville;
	         
	         }
	         
	         if(isset($_POST['CP'])){
	            while($val = mysql_fetch_assoc($result_ville)){
	               $tab_ville = $val['cp'];
	            }
	         
	            $tab_get_exp[$compte]['CPCommune'] = $tab_ville;
	         
	         }
	         
	         
	         mysql_free_result($result_ville);
	      
	      }
	
	   
	     if(isset($_POST['Pays'])){
	   		$query_pays = "SELECT p.NOMPAYS as nom FROM pays p ";
	   		$query_pays .= "INNER JOIN commune c ON c.CODEPAYS = p.CODEPAYS ";
	         $query_pays .= "WHERE c.CODECOMMUNE = ".$tab['idVille']." ";
	      
	         $result_pays = mysql_query($query_pays, $connexion) or logError("RECUPERATION  PAYS-".$query_pays."-".mysql_error());
	         
	         $tab_pays = false;
	         
	         while($val = mysql_fetch_assoc($result_pays)){
	            $tab_pays = $val['nom'];
	         }
	         
	         $tab_get_exp[$compte]['nomPays'] = $tab_pays;
	         
	         mysql_free_result($result_pays);
	   	}
	   	
	      if(isset($_POST['LastC'])){
	         $tab_get_exp[$compte]['lastC'] = $tab['lastC'];
	      }
	      
	      if(isset($_POST['Tel'])){
	         $tab_get_exp[$compte]['tel'] = $tab['tel'];
	      }
	      
	      if(isset($_POST['Fax'])){
	         $tab_get_exp[$compte]['fax'] = $tab['fax'];
	      }
	      
	      if(isset($_POST['Email'])){
	         $tab_get_exp[$compte]['email'] = $tab['email'];
	      }
	      
	      if(isset($_POST['Web'])){
	         $tab_get_exp[$compte]['web'] = $tab['web'];
	      }
	      
	      
	      $compte++;
	   }   
	     
	   mysql_free_result($result_get_structures);
   }
   foreach ($entete as $elem) {
      echo "\"$elem\",";
   }
   echo "\n";
   
   for($i=0;$i<$compte;$i++){
      foreach ($tab_get_exp[$i] as $elem) {
         echo "\"$elem\",";
      }
      echo "\n";
   }
   
   $time_end = microtime(true);
   echo "Time : ".($time_end-$time_start)." sec";
   


?>